package leetcode101.ch3;
//LC 76 最小覆盖子串
//给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串，则返回空字符串 ""

public class MinWindowSubString {
    public String minWindow(String s, String t) {
        String res = "";
        int[] src = new int[256];
        for (int i = 0; i < t.length(); i++) {
            src[t.charAt(i)]++;
        }
        int count = t.length();
        for (int l = 0, r = -1; l < s.length();) {
            if (r < s.length() -1 && count > 0) {
                if(src[s.charAt(++r)]-- > 0){
                    count--;
                }
            }else if(src[s.charAt(l++)]++ >= 0){
                count++;
            }
            if (count == 0) {
                System.out.println("l:"+l + " r:"+r);
                String tmp = s.substring(l, r + 1);
                if (res == "" || res.length() > tmp.length()) {
                    res = tmp;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        String s = "ADOBECODEBANC", t = "ABC";
        System.out.println(new MinWindowSubString().minWindow(s, t));
    }
}
